/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.hcht.xrlh.rw.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.hcht.xrlh.drhis.entity.DrHis;
import com.hcht.xrlh.drhis.service.DrHisService;
import com.hcht.xrlh.rw.entity.DrRw;
import com.hcht.xrlh.rw.service.DrRwService;
import com.hcht.xrlh.rwfz.entity.DrRwXz;
import com.hcht.xrlh.rwfz.entity.DrRwXzyr;
import com.hcht.xrlh.rwfz.entity.DrXzRw;
import com.hcht.xrlh.rwfz.entity.ShowTable;
import com.hcht.xrlh.rwfz.service.DrRwXzService;
import com.hcht.xrlh.sx.entity.DrSx;
import com.hcht.xrlh.sx.service.DrSxService;
import com.hcht.xrlh.zfdxk.entity.DrZfdxk;
import com.hcht.xrlh.zfdxk.service.DrZfdxkService;
import com.hcht.xrlh.zfdxktitle.entity.DrZfdxkTitle;
import com.hcht.xrlh.zfdxktitle.service.DrZfdxkTitleService;
import com.hcht.xrlh.zfryk.entity.DrZfryk;
import com.hcht.xrlh.zfryk.service.DrZfrykService;

/**
 * 检查任务Controller
 * @author lrz
 * @version 2016-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/rw/drRw")
public class DrRwController extends BaseController {

	@Autowired
	private DrRwService drRwService;
	
	@Autowired
	private DrSxService drSxService;
	
	@Autowired
	private DrZfrykService drZfrykService;
	
	@Autowired
	private DrZfdxkService drZfdxkService;
	
	@Autowired
	private DrZfdxkTitleService drZfdxkTitleService;
	
	@Autowired
	private DrRwXzService drRwXzService;
	
	@Autowired
	private DrHisService drHisService;
	
	
	@ModelAttribute
	public DrRw get(@RequestParam(required=false) String id) {
		DrRw entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drRwService.get(id);
		}
		if (entity == null){
			entity = new DrRw();
		}
		return entity;
	}
	
	@RequiresPermissions("rw:drRw:view")
	@RequestMapping(value = {"list", ""})
	public String list(DrRw drRw, HttpServletRequest request, HttpServletResponse response, Model model) {
		for(Role role : UserUtils.getUser().getRoleList()){
			if("1".equals(role.getId())){
			}else{
				drRw.setOfficeZd(UserUtils.getUser().getOffice());
			}
		}
		//drRw.setOfficeZd(UserUtils.getUser().getOffice());
		Page<DrRw> page = drRwService.findPage(new Page<DrRw>(request, response), drRw); 
		
		model.addAttribute("page", page);
		return "xrlh/rw/drRwList";
	}

	@RequiresPermissions("rw:drRw:view")
	@RequestMapping(value = "form")
	public String form(DrRw drRw, Model model,HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("type"); 
		String sxId = request.getParameter("sxId"); 
		String jhId = request.getParameter("jhId"); 
		drRw.setSxId(sxId);
		drRw.setJhId(jhId);
		
		DrSx sx = drSxService.get(sxId);
		drRw.setRwFdsx(sx.getSxFdsx());
		
		DrZfryk ry = new DrZfryk();
		ry.setSxId(sxId);
		//Page<DrZfryk> page_ryk = drZfrykService.findPage(new Page<DrZfryk>(request, response), ry); 
		List<DrZfryk> list_ryk = drZfrykService.findList(ry);
		
		DrZfdxk dx = new DrZfdxk();
		dx.setDrSx(sx);
		List<DrZfdxk> list_dxk = drZfdxkService.findList(dx);
		//Page<DrZfdxk> page_dxk = drZfdxkService.findPage(new Page<DrZfdxk>(request, response), dx); 
		List<DrZfdxkTitle> list_title = Lists.newArrayList();
		DrZfdxkTitle title = new DrZfdxkTitle();
		title.setSxId(sxId);
		list_title = drZfdxkTitleService.findList(title);
		
		model.addAttribute("list_title", list_title);
		model.addAttribute("page_ryk", list_ryk);
		model.addAttribute("page_dxk", list_dxk);
		model.addAttribute("drRw", drRw);
		model.addAttribute("type", type);
		return "xrlh/rw/drRwForm";
	}

	@RequiresPermissions("rw:drRw:view")
	@RequestMapping(value = "toList")
	public String toList(DrRw drRw, Model model,HttpServletRequest request, HttpServletResponse response) {
		
		DrRwXz drRwXz = new DrRwXz();
		drRwXz.setRwId(drRw.getId());
		List<DrRwXz> list_xz = drRwXzService.findList(drRwXz);
		List<DrRwXz> list_xz_ = Lists.newArrayList();
		List<DrXzRw> dxk = Lists.newArrayList();
		for(DrRwXz xz:list_xz){
			xz = drRwXzService.get(xz.getId());
			list_xz_.add(xz);
			for(DrXzRw rw:xz.getDrXzRwList()){
				dxk.add(rw);
			}
		}
		
		List<DrZfdxkTitle> list_title = Lists.newArrayList();
		DrZfdxkTitle title = new DrZfdxkTitle();
		title.setSxId(drRw.getSxId());		
		list_title = drZfdxkTitleService.findList(title);
		
		model.addAttribute("drRw", drRw);
		model.addAttribute("list_xz", list_xz_);
		model.addAttribute("list_title", list_title);
		model.addAttribute("dxk", dxk);
		return "xrlh/rw/listok";
	}
	/*
	 * 新增
	 * 任务表DR_RW
	 * 任务小组DR_RW_XZ
	 * 任务小组人员DR_RW_XZYR
	 * 小组任务DR_XZ_RW
	 * 抽查结果DR_JG
	 * */
	@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "save")
	public String save(DrRw drRw, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		
		if (!beanValidator(model, drRw)){
			return form(drRw, model,request,response);
		}

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		DrSx sx = drSxService.get(drRw.getSxId());
		
		String uids = request.getParameter("uids");
		drRw.setRwFlag("-1");
		drRw.setRwFdsx(sx.getSxFdsx());
		drRw.setRwMc(sx.getSxMc()+"计划-"+f.format(new Date()));
		drRw.setOfficeZd(UserUtils.getUser().getOffice());
		
		/*
		 * 执法人员、执法对象数量验证
		 * */
		List<String> check = drRwService.checkRyDx(drRw, uids);
		/*
		 * */
		if(check.size()>0){
			redirectAttributes.addAttribute("type", "error");
			for(String msg:check){
				addMessage(redirectAttributes, msg);
				
			}
			//return form(drRw, model,request,response);
			return "redirect:"+Global.getAdminPath()+"/rw/drRw/form?type=error&sxId="+drRw.getSxId()+"&jhId="+drRw.getJhId();
			//return "redirect:"+Global.getAdminPath()+"/rw/drRw/?repage";
			//return "xrlh/rw/drRwForm";
		}
		String rwid = drRwService.save(drRw,uids);
		//addMessage(redirectAttributes, "随机抽取成功");
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/toList?id="+rwid;
	}
	
	@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "delete")
	public String delete(DrRw drRw, RedirectAttributes redirectAttributes) {
		drRwService.delete(drRw);
		addMessage(redirectAttributes, "删除检查任务成功");
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/?repage";
	}

	@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "saveZz")
	public String saveZz(DrRw drRw, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {		
		String rwid= "";
		String[] zzs = request.getParameter("zzs").split(",");
		for(int i=0;i<zzs.length;i++){
			DrRwXzyr ry = drRwXzService.getRwXzyr(zzs[i]);
			ry.setZz("1");
			drRwXzService.updatezz(ry);
			rwid = ry.getRwId();
		}
		addMessage(redirectAttributes, "分组组长保存成功");
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/result_?rwid="+rwid;
	}
	
	//@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "result_")
	public String result_(DrRw drRw, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {		
		String rwid= request.getParameter("rwid");
		DrRw rw = drRwService.get(rwid);
		List<ShowTable> list =  Lists.newArrayList();
		DrRwXz drRwXz = new DrRwXz();
		drRwXz.setRwId(rw.getId());
		List<DrRwXz> xz_list = drRwXzService.findList(drRwXz);
		for(DrRwXz rwxz:xz_list){
			String names = "";
			String dxs = "";
			ShowTable st = new ShowTable();
			st.setGroupName(rwxz.getRemarks());
			rwxz = drRwXzService.get(rwxz.getId());
			for(DrRwXzyr ry :rwxz.getDrRwXzyrList()){
				names = names+ry.getUser().getName()+"、";
				if("1".equals(ry.getZz())){
					st.setCaptain(ry.getUser().getName());
				}
			}
			if(names.indexOf("、")>0){
				names = names.substring(0,names.lastIndexOf("、"));
			}
			st.setUsers(names);
			for(DrXzRw xzrw:rwxz.getDrXzRwList()){
				dxs = dxs+xzrw.getCol1()+"、";
			}
			if(dxs.indexOf("、")>0){
				dxs = dxs.substring(0,dxs.lastIndexOf("、"));
			}
			st.setUsers(names);
			st.setObjects(dxs);
			list.add(st);
		}
		model.addAttribute("st", list);
		model.addAttribute("drRw", rw);
		return "xrlh/rw/confirmResult";
	}
	
	@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "modifyflag")
	public String modifyflag(DrRw drRw, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {		
		drRw.setRwFlag("00");
		drRwService.update(drRw);
		//更新抽取历史
		DrHis para = new DrHis();
		para.setRwId(drRw.getId());
		List<DrHis> drHisList = drHisService.findList(para);
		if(drHisList.size()>0){
			DrHis drHis = drHisList.get(0);
			drHis.setFlag("y");
			drHisService.save(drHis);
		}
		addMessage(redirectAttributes, "任务制定成功");
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/list";
	}
	
	@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "updateFlag")
	public String updateFlag(DrRw drRw, RedirectAttributes redirectAttributes) {
		drRw.setRwFlag("02");
		drRwService.update(drRw);
		addMessage(redirectAttributes, "操作成功");
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/list";
	}	
	
	@RequiresPermissions("rw:drRw:edit")
	@RequestMapping(value = "doExtract")
	public String doExtract(DrRw drRw, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request, HttpServletResponse response) {
		//drRw = drRwService.get(drRw.getId());
		drRw.setRwFlag("-2");
		drRw.setDelFlag("1");
		drRwService.update(drRw);
		drRwService.delete(drRw);
		redirectAttributes.addAttribute("sxId", drRw.getSxId());
		redirectAttributes.addAttribute("jhId", drRw.getJhId());
		return "redirect:"+Global.getAdminPath()+"/rw/drRw/form";
	}	
}